package com.zhangdi.mgsync.core.config;

import com.mongodb.client.MongoClients;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.SpringProperties;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.SpringDataMongoDB;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2023.12.5
 */
@Configuration
public class MongoConfiguration {
//    @Value("${spring.data.mongodb.uri}")
//    String uri;

    @Bean(name = "tenderMongoProperties")
    @ConfigurationProperties(prefix = "spring.data.mongodb.tender")
    public MongoProperties tenderMongoProperties() {
        return new MongoProperties();
    }

    @Primary
    @Bean(name = "onlineMongoProperties")
    @ConfigurationProperties(prefix = "spring.data.mongodb.online")
    public MongoProperties onlineMongoProperties() {
        return new MongoProperties();
    }

    @Bean(name = "standardMongoProperties")
    @ConfigurationProperties(prefix = "spring.data.mongodb.standard")
    public MongoProperties standardMongoProperties() {
        return new MongoProperties();
    }

    @Bean("tenderMongoTemplate")
    public MongoTemplate tenderMongoTemplate(@Qualifier("tenderMongoProperties") MongoProperties properties) {
        return new MongoTemplate(MongoClients.create(properties.getUri()), properties.getDatabase());
    }

    @Primary
    @Bean("onlineMongoTemplate")
    public MongoTemplate onlineMongoTemplate(@Qualifier("onlineMongoProperties") MongoProperties properties) {
        return new MongoTemplate(MongoClients.create(properties.getUri()), properties.getDatabase());
    }

    @Bean("standardMongoTemplate")
    public MongoTemplate standardMongoTemplate(@Qualifier("standardMongoProperties") MongoProperties properties) {
        return new MongoTemplate(MongoClients.create(properties.getUri()), properties.getDatabase());
    }

//    @Bean("tenderMongoDatabaseFactory")
//    public MongoDatabaseFactory tenderMongoDatabaseFactory(MongoProperties tenderMongoProperties) {
//        return new SimpleMongoClientDatabaseFactory(MongoClients.create(uri), tenderMongoProperties.getDatabase());
//    }
//
//    @Bean("onlineMongoDatabaseFactory")
//    public MongoDatabaseFactory onlineMongoDatabaseFactory(MongoProperties onlineMongoProperties) {
//        return new SimpleMongoClientDatabaseFactory(MongoClients.create(uri), onlineMongoProperties.getDatabase());
//    }
}
